﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Common;
using JDEduTrain.Framework.Entity;
using JDEduTrain.Framework.Business;
using Wicresoft.Framework.Organization;
using NameControlLib;

public partial class Page_ClassMemberList : BaseAuthenticationPage
{
    public Guid ClassID
    {
        get
        {
            if (Request["ClassID"] != null)
                return new Guid(Request["ClassID"]);
            return Guid.Empty;
        }
    }
    public bool IsSelectClass
    {
        get
        {
            if (Request["IsSelectClass"] != null)
                return bool.Parse(Request["IsSelectClass"]);
            return false;
        }
    }

    public string ApplyOrganID
    {
        get
        {
            if (ViewState["ApplyOrganID"] != null)
                return ViewState["ApplyOrganID"].ToString();
            return string.Empty;
        }
        set
        {
            ViewState["ApplyOrganID"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.UseTabber = true;
        if (!IsPostBack)
        {
            BindClassMember();

            DataTable dtOrder = BLTrain.GetOrderByClassID(ClassID);
            foreach (DataRow dr in dtOrder.Rows)
            {
                ClassOrder order = new ClassOrder();
                order.Init(dr);
                string orderStr = order.BeginTime.ToShortDateString() + "至" + order.EndTime.ToShortDateString() + ";" + order.BeginHour + "至" + order.EndHour + ";";
                ddlOrder.Items.Add(new ListItem(orderStr, order.ID.ToString()));

            }
            if (IsSelectClass)
            {
                tblOperator.Visible = false;
                lhcbUser.Visible = false;
            }
            TrainClass trainClass = new TrainClass(ClassID);
            if (trainClass.Load())
            {
                ApplyOrganID = trainClass.ApplyOrganID;
            }
        }
    }

    protected DataTable DTUserPageList
    {
        get
        {
            if (ViewState["DTUserPageList"] != null)
            {
                return (DataTable)ViewState["DTUserPageList"];
            }
            return new DataTable();
        }
        set
        {
            ViewState["DTUserPageList"] = value;
        }
    }


    protected void BindClassMember()
    {
        int recordCount =0;
        if (IsSelectClass)
        {
            thInfo.Title = "已报名学员";
            DTUserPageList = BLTrain.GetMemberByClassID(ClassID,dpPager.CurrentPageIndex,dpPager.PageSize,"1=1",ClassMember.SQLCOL_MEMBERNAME + " ASC",ref recordCount);
        }
        else
        {
            string memeberStatus = "终审通过";
            DTUserPageList = BLTrain.GetMemberByClassID(ClassID,memeberStatus, dpPager.CurrentPageIndex, dpPager.PageSize, "1=1", ClassMember.SQLCOL_MEMBERNAME + " ASC", ref recordCount);
        }
        DTUserPageList.Columns.Add("OrderName");
        DTUserPageList.Columns.Add("ThirdLevelDeptDisplayName");
        foreach (DataRow dr in DTUserPageList.Rows)
        {
            ClassMember member = new ClassMember();
            member.Init(dr);
            UserExtend userEx = new UserExtend(member.ID);
            if(userEx.Load())
            {
                dr["ThirdLevelDeptDisplayName"] = userEx.ThirdLevelDeptDisplayName;
            }

        }
        rptOrderInfo.DataSource = DTUserPageList;
        rptOrderInfo.DataBind();
        dpPager.RecordCount = recordCount;

    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindClassMember();
    }

    protected void btnAddUser_Click(object sender, EventArgs e)
    {
        //string[] userIDs = tbUserID.Text.Split(';');
        //foreach (string userID in userIDs)
        //{
        //    if (!string.IsNullOrEmpty(userID))
        //    {
        //        BLTrain.AddClassMemberById(new Guid(userID),ClassID,OrganizationHelper.IsUserInRole(CurrentUser.ID,AppConfig.OrganizeAdminRoleID));
        //    }
        //}
        //tbUserDisplayName.Text = "";
        //tbUserID.Text = "";
        //tbUserName.Text = "";
        //BindClassMember();
    }

    protected void btnAddApplyUser_Click(object sender, EventArgs e)
    {

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ArrayList alUser = new ArrayList();

        for (int i = 0; i < rptOrderInfo.Items.Count; i++)
        {
            if ((rptOrderInfo.Items[i].FindControl("cbUser") as CheckBox).Checked)
            {
                Guid id = new Guid(DTUserPageList.Rows[rptOrderInfo.Items[i].ItemIndex]["ID"].ToString());
                alUser.Add(id);
            }
        }
        BLTrain.DeleteClassMember((Guid[])alUser.ToArray(typeof(Guid)), ClassID);
        BindClassMember();
    }
    protected void btnAttendClass_Click(object sender, EventArgs e)
    {
        ArrayList alUser = new ArrayList();
        string script;
        for (int i = 0; i < rptOrderInfo.Items.Count; i++)
        {
            if ((rptOrderInfo.Items[i].FindControl("cbUser") as CheckBox).Checked)
            {
                Guid id = new Guid(DTUserPageList.Rows[rptOrderInfo.Items[i].ItemIndex]["ID"].ToString());
                alUser.Add(id);
            }
        }
        if (ddlOrder.SelectedValue != "请选择")
        {
            BLTrain.MoveClassMember((Guid[])alUser.ToArray(typeof(Guid)), ClassID, new Guid(ddlOrder.SelectedValue));
            script = "<script>alert('调整完成！')</script>";
        }
        else
        {
            script = "<script>alert('请选择班次！')</script>";
        }
        ClientScript.RegisterStartupScript(this.GetType(), "Close", script);
        BindClassMember();
    }

    protected void rptOrderInfo_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HtmlTableCell cell = e.Item.FindControl("tdCb") as HtmlTableCell;
            CheckBox cb = e.Item.FindControl("cbUser") as CheckBox;
            if (IsSelectClass)
            {
                cell.Visible = false;
            }
            Label lFullName = e.Item.FindControl("lFullName") as Label;


            string id = DTUserPageList.Rows[e.Item.ItemIndex]["ID"].ToString();
            NameControlLib.NameInfo userControl = new NameInfo();
            userControl.ID = "UserNameControl_" + id;
            userControl.UserID = new Guid(id);
            userControl.ScriptPath = "../Js/NameInfo.js";
            lFullName.Controls.Add(userControl);
        }
    }

    protected void dpPager_PreRender(object sender, EventArgs e)
    {
        BindClassMember();
    }
}
